//
//  CustomCellView.m
//  test
//
//  Created by 郭子立 on 2017/10/19.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "CustomCellView.h"
#import "TCollectionViewCell.h"


#define Size_width [UIScreen mainScreen].bounds.size.width
#define Size_height [UIScreen mainScreen].bounds.size.height

@interface CustomCellView ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,HistoryWorkFlowsTableViewCellDelegate>

@property (strong, nonatomic) UICollectionView *CollectionView;
@property (strong, nonatomic) UIScrollView *backScrollView;
@property (assign, nonatomic) CGRect viewFrame;
@property (assign, nonatomic) NSInteger yNameCount;
@property (assign, nonatomic) NSInteger xNameCount;

@end

@implementation CustomCellView


-(instancetype)initWithFrame:(CGRect)frame withLeftNameArray:(NSArray*)leftNameArray withTopNameArray:(NSArray*)topNameArray {

    if ([super initWithFrame:frame]) {
        
        _xNameCount = leftNameArray.count;
        _yNameCount = topNameArray.count;
        
        _viewFrame = frame;
        NSInteger hightName = frame.size.height / _xNameCount;
        
        UILabel *Titlelabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, 0, 110, hightName))];
        Titlelabel.textAlignment = NSTextAlignmentCenter;
        Titlelabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:Titlelabel];
        for (int i = 1; i < leftNameArray.count; i++) {
            
            UILabel *nameLabe = [[UILabel alloc] initWithFrame:(CGRectMake(0, hightName*(i), 110, hightName))];
            nameLabe.textAlignment = NSTextAlignmentCenter;
            nameLabe.font = [UIFont systemFontOfSize:13];
            nameLabe.text = [NSString stringWithFormat:@"%@", [leftNameArray objectAtIndex:i]];
            [self addSubview:nameLabe];
        }
        
        self.backScrollView = [[UIScrollView alloc] initWithFrame:(CGRectMake(110, 0, Size_width-110, frame.size.height))];
        self.backScrollView.showsHorizontalScrollIndicator = NO;
        self.backScrollView.contentSize = CGSizeMake(110*(_yNameCount -1), 0);
        self.backScrollView.delegate = self;
        [self addSubview:self.backScrollView];
        
        
        for (int i = 1; i < topNameArray.count; i++) {
            UILabel *nameLabe = [[UILabel alloc] initWithFrame:(CGRectMake(110*(i-1), 0, 110, hightName))];
            nameLabe.textAlignment = NSTextAlignmentCenter;
            nameLabe.text = [NSString stringWithFormat:@"%@", [topNameArray objectAtIndex:i]];
            nameLabe.font = [UIFont systemFontOfSize:13];
            [self.backScrollView addSubview:nameLabe];
        }

        [self.backScrollView addSubview:self.CollectionView];

    }
    return self;
}


- (UICollectionView *)CollectionView {
    
    if (!_CollectionView) {
        
        UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
        
        NSInteger hightName = self.frame.size.height / _xNameCount;
        flowLayOut.itemSize = CGSizeMake(110-2, hightName-2);
        
        flowLayOut.minimumInteritemSpacing = 0;
        
        flowLayOut.minimumLineSpacing = 1;
        
        flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        
        flowLayOut.sectionInset = UIEdgeInsetsMake(1, 1, 0, 0);
        
        
        
        _CollectionView = [[UICollectionView alloc]initWithFrame:
                           CGRectMake(0, hightName, self.backScrollView.contentSize.width, self.frame.size.height-hightName) collectionViewLayout:flowLayOut];
        _CollectionView.backgroundColor = [UIColor whiteColor];
        
        _CollectionView.dataSource = self;
        
        _CollectionView.delegate = self;
        
        [_CollectionView registerClass:[TCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        
        
    }
    
    
    return _CollectionView;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return (_yNameCount-1);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return _xNameCount-1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    TCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    [cell arrangeCellIndex:indexPath.section section:indexPath.row];

    return cell;
}



@end
